package cn.iocoder.yudao.module.call.controller.admin.acsnlpresult;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.call.controller.admin.acsnlpresult.vo.*;
import cn.iocoder.yudao.module.call.dal.dataobject.acsnlpresult.AcsNlpResultDO;
import cn.iocoder.yudao.module.call.service.acsnlpresult.AcsNlpResultService;

@Tag(name = "管理后台 - 对话记录")
@RestController
@RequestMapping("/call/acs-nlp-result")
@Validated
public class AcsNlpResultController {

    @Resource
    private AcsNlpResultService acsNlpResultService;

    @PostMapping("/create")
    @Operation(summary = "创建对话记录")
    @PreAuthorize("@ss.hasPermission('call:acs-nlp-result:create')")
    public CommonResult<Integer> createAcsNlpResult(@Valid @RequestBody AcsNlpResultSaveReqVO createReqVO) {
        return success(acsNlpResultService.createAcsNlpResult(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新对话记录")
    @PreAuthorize("@ss.hasPermission('call:acs-nlp-result:update')")
    public CommonResult<Boolean> updateAcsNlpResult(@Valid @RequestBody AcsNlpResultSaveReqVO updateReqVO) {
        acsNlpResultService.updateAcsNlpResult(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除对话记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('call:acs-nlp-result:delete')")
    public CommonResult<Boolean> deleteAcsNlpResult(@RequestParam("id") Integer id) {
        acsNlpResultService.deleteAcsNlpResult(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得对话记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('call:record:ivr')")
    public CommonResult<AcsNlpResultRespVO> getAcsNlpResult(@RequestParam("id") Integer id) {
        AcsNlpResultDO acsNlpResult = acsNlpResultService.getAcsNlpResult(id);
        return success(BeanUtils.toBean(acsNlpResult, AcsNlpResultRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得对话记录分页")
    @PreAuthorize("@ss.hasPermission('call:record:ivr')")
    public CommonResult<PageResult<AcsNlpResultRespVO>> getAcsNlpResultPage(@Valid AcsNlpResultPageReqVO pageReqVO) {
        PageResult<AcsNlpResultDO> pageResult = acsNlpResultService.getAcsNlpResultPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AcsNlpResultRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得对话记录列表")
    @PreAuthorize("@ss.hasPermission('call:record:ivr')")
    public CommonResult<List<AcsNlpResultRespVO>> getAcsNlpResultSimpleList(@Valid AcsNlpResultPageReqVO pageReqVO) {
        List<AcsNlpResultDO> acsNlpResultSimpleList = acsNlpResultService.getAcsNlpResultSimpleList(pageReqVO);
        return success(BeanUtils.toBean(acsNlpResultSimpleList, AcsNlpResultRespVO.class));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出对话记录 Excel")
    @PreAuthorize("@ss.hasPermission('call:acs-nlp-result:export')")
    @OperateLog(type = EXPORT)
    public void exportAcsNlpResultExcel(@Valid AcsNlpResultPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AcsNlpResultDO> list = acsNlpResultService.getAcsNlpResultPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "对话记录.xls", "数据", AcsNlpResultRespVO.class,
                BeanUtils.toBean(list, AcsNlpResultRespVO.class));
    }

}